<?php 
require X_PATH . '/Base/Model.php';
header('Access-Control-Allow-Origin:*');  
header('Access-Control-Allow-Credentials: true');
header("Content-type:text/html;charset=utf-8");

header('Access-Control-Allow-Methods:OPTIONS, GET, POST');
header('Access-Control-Max-Age:86400');  
header('Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header('Access-Control-Allow-Headers:Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With');

class GoodsController extends Controller
{
    public function goodsCat()
    {
        $goodsCat = new goodsCatModel();

        if(isset($_GET['type']) && $_GET['type'] == 'post'){
            $goodsCat->gc_name = $_REQUEST['gc_name'];
            $goodsCat->gc_taxi = isset($_REQUEST['gc_taxi']) ? $_REQUEST['gc_taxi'] : 255;
            if($goodsCat->add()){
                echo json_encode($goodsCat->succ);
            }
        }else{
            $data = $goodsCat->select();
            echo json_encode($data); 
        }
    }

    public function goodsimg()
    {
        $goodsimg = new goodsimgModel();
        $goods_img = $goodsimg->uploadFile($_FILES['goods_img'],'./uploads/goods',200000,['jpg','png']);
        echo json_encode($goods_img);
    }

    public function goods()
    {
        $goods = new goodsModel();
        if(isset($_GET['type']) && $_GET['type'] == 'post'){
            $goods->gc_id = $_REQUEST['gc_id'];
            $goods->gname = $_REQUEST['gname'];
            $goods->market_price = $_REQUEST['market_price'];
            $goods->shop_price = $_REQUEST['shop_price'];
            $goods->promote_price = $_REQUEST['promote_price'];
            $goods->goods_desc = $_REQUEST['goods_desc'];

            $goods->index_img = $_REQUEST['index_img'];

            //$goods->goods_img = $_REQUEST['goods_img'];

            $goods->integral = $_REQUEST['integral'];
            $goods->preOrder = $_REQUEST['preOrder'];
            $goods->deduction  = $_REQUEST['deduction'];
            $goods->deduction_number  = $_REQUEST['deduction_number'];
            $goods->freight_charge = $_REQUEST['freight_charge'];
            $goods->is_putaway = $_REQUEST['is_putaway'];

            if($goods->add()){
                echo json_encode($goods->succ);
            }
        }else{
            $data = $goods->select();
            echo json_encode($data);
        }
    }

    public function articleCat()
    {
        $articleCat = new articleCatModel();
        if(isset($_GET['type']) && $_GET['type'] == 'post'){
            $articleCat->ac_name = $_REQUEST['ac_name'];
            $articleCat->ac_taxi = isset($_REQUEST['ac_taxi']) ? $_REQUEST['ac_taxi'] : 255;
            if($articleCat->add()){
                echo json_encode($articleCat->succ);
            }
        }else{
            $data = $articleCat->select();
            echo json_encode($data);
        }
    }

    public function article()
    {
        $article = new articleModel();
        if(isset($_GET['type']) && $_GET['type'] == 'post'){
            $article->ac_id = isset($_REQUEST['ac_id']) ? $_REQUEST['ac_id'] : exit('选择文章分类');
            $article->title = isset($_REQUEST['title']) ? $_REQUEST['title'] : exit('填写文章标题');
            $article->content = $_REQUEST['content'];
            $article->author = isset($_REQUEST['author']) ? $_REQUEST['author'] : exit('填写作者'); 
            $article->keywords = $_REQUEST['keywords'];
            $article->is_open = isset($_REQUEST['is_open']) ? $_REQUEST['is_open'] : 1;
            $article->sort = isset($_REQUEST['sort']) ? $_REQUEST['sort'] : 255;
            $article->add_time = isset($_REQUEST['add_time']) ? $_REQUEST['add_time'] : time();
            $article->description = $_REQUEST['description'];

            if($article->add()){
                echo json_encode($article->succ);
            }
        }else{
            $data = $article->select();
            echo json_encode($data);
        }
    }

    public function banner()
    {
        $banner = new bannerModel();
        if(isset($_GET['type']) && $_GET['type'] == 'post'){
            $banner_img = '';
            foreach ($_FILES as $key => $value) {
                $banner_img = $banner->uploadFile($_FILES[$key],'./uploads/banner',200000,['img','jpg']);
                echo json_encode($banner_img);
            }

            $banner->banner_img = $banner_img['dest'];
            $banner->banner_title = $_REQUEST['banner_title'];
            $banner->banner_link = $_REQUEST['banner_link'];
            $banner->banner_sort = isset($_REQUEST['banner_sort']) ? $_REQUEST['banner_sort'] : 255;
           
            if($banner->add()){
                echo json_encode($banner->succ);
            }
        }else{
            $data = $banner->select();
            echo json_encode($data);
        }
    }

    public function orderGoods()
    {
        $orderGoods = new orderGoodsModel();
        $data = $orderGoods->select();
        echo json_encode($data);
    }

    public function user()
    {
        $user = new userModel();
        $data = $user->select();
        echo json_encode($data);
    }

    public function userAddress()
    {
        $userAddress = new userAddressModel();
        $data = $userAddress->select();
        echo json_encode($data);
    }

    public function delete()
    {
        $delete = new deleteModel();
        $id = $_REQUEST['id'];
        if(empty($id)){
            exit('你想干啥');
        }
        $name = $_REQUEST['name'];
        $t = $_REQUEST['table'];

    	$data = $delete->remove($t,$id,$name); 
    	echo $data;

    }
}















 ?>
